<template>
	<view class="mian">
		<!-- #ifdef MP-WEIXIN -->
		<view class="class_h" style="background-color: #fff">
			<view class="top" @click="return_step">					
				<image src="../../static/resource/back.png" mode="aspectFill" style="width: 74px;height: 30px;"></image>				
			</view>
			<text class="task_name">订单管理</text>
		</view>	
		<!-- #endif -->	
		<view style="padding-bottom: 20px;">		
			
		<u-tabs :list="list1" :current="current" @click="changeNav()" :activeStyle="{
            color: '#36B3E9',
            fontWeight: 'bold',
            transform: 'scale(1.05)'
        }"    lineColor="#36B3E9"  v-if="tabbar==1"></u-tabs>
		
		<u-tabs :list="list2" @click="changeNav()" :activeStyle="{
            color: '#36B3E9',
            fontWeight: 'bold',
            transform: 'scale(1.05)'
        }"   lineColor="#36B3E9" :current="current" v-if="tabbar==2"></u-tabs>		
		<swiper class="tab-content"  duration="300" bindchange="changeTab" :style="{'height':winHeight+'px'}">	          
				<swiper-item>
					<scroll-view :scroll-y="true" class="new-list-box" @scrolltolower="allLoadMore" scroll-top="scrollTop" scroll-with-animation :refresher-enabled="true" refresher-threshold="80" refresher-default-style="white" refresher-background="white" :refresher-triggered="triggered" @refresherrefresh="onAllRefresh">
					  <block v-if="allTabList.length > 0"  v-for="(goods,index1) in allTabList"   :key="index1">
						  <view style="background: #fff;margin-bottom: 20rpx;pading-right: 20rpx;padding-left: 20rpx;border-radius: 16rpx 16rpx 16rpx 16rpx;" class="ml-20 mr-20">
							<view class="order_main" @click="goDetails(goods.id)">
								<view class="flex justify-between ml-25 mr-25 text-dff pt-20 border-b pb-15">	                         
									<view class="pt-10 text-df" style="color: #000;;">{{goods.pay_no}}</view>
									<view style='color: gray;margin-top: 10rpx;'    v-if="goods.status==0">已取消</view>		
									<view style="color: #36B3E9 ; margin-top: 10rpx;" v-if="goods.status==2">待发货</view>						
									<view style='color: #888888  ;margin-top: 10rpx;' v-if="goods.status==3">待收货</view>		
									<view style='color: #92BBC2 ;margin-top: 10rpx;' v-if="goods.status==4">已完成</view>									  
									<view style='color: #000;margin-top: 10rpx;'   v-if="goods.status==5">
									  {{goods.sales_status == 1?"售后中":goods.sales_status == 2?"退款成功":"退款失败"}}
									  
									</view>
									<view style='color: red;margin-top: 10rpx;'    v-if="goods.status==6">租用中</view>								  
									<view style='color: red;margin-top: 10rpx;'    v-if="goods.status==7">归还中</view>	
								</view>  								
								<view class="order_list flex flex-wrap">
									<block v-for="(item,index) in goods.goods" :key="index">										
										<view class="flex mt-10 mb-10 " style="width: 100%;align-items: center;">
											  <view slot='icon' class="flex align-center" >
												<image class="grid-img" :src="item.goods_logo" bindtap="handleChangeGoods" 	mode="scaleToFill"></image>
											  </view>								  
											  <view class="flex flex-direction ml-10" style="width: 100%;">
												<view  style="width: 100%;">
												  <view class='flex align-center cell text-lg mb-10' style="color: #011F37;">{{item.goods_title}}</view>
												  <view class='flex  text-sm text-gray mb-10 mr-25'>
												   <text>{{item.goods_spec}}</text>												   
												  </view>
												</view>												
												<view class='flex justify-between mr-20' style="margin-top: 10rpx;">
												  <view class="">
												  	<text class="text-red" style="font-weight: bold;font-size: 40rpx;color: #FF2E00;">￥{{item.selling_price}}</text>  
												  </view>
													
												</view>
											 </view>
										</view>
									</block>                   
							   </view>	 					   
								<view class="pay_price  pt-30 mt-20">
									 <text class="mr-20" style="color: #000000;">付款:</text>
									 <text style="color: #ED1C24;font-weight: 600;">¥ {{goods.pay_price}}</text>
								</view>
							</view>								
							<view class="flex pb-15 mt-10 mr-20 justify-between align-center text-center" >		
								<view class="flex">
									<text style="font-size: 24rpx;color: #888888;">{{goods.create_at}}</text>
								</view>
								<view class="flex">
															
									
									<view class="mt-20 ml-20" v-if="goods.status ==3">
										<u-button text="查看物流" shape="circle" type="primary"  :plain="true" size="small" color="#707070" class="ml-20" customStyle="width:150rpx;height:65rpx" @click="paykd(goods.id)" ></u-button>
									</view>									
												
									
									<view class="mt-20 ml-10">
										<u-button text="立即评价" shape="circle" type="primary" color="#36B3E9" size="small" class="ml-20" customStyle="width:150rpx;height:65rpx"  @click="doevaluate(goods.id)" v-if="(goods.status==4 && goods.isEva == 0)"></u-button>
									</view>
									<view class="mt-20 ml-20" v-if="goods.status==3">
										<u-button text="确认收货" shape="circle" type="primary"  size="small" color="#36B3E9;" class="ml-20" customStyle="width:150rpx;height:65rpx" @click="payok(goods.id,goods.type)" ></u-button>
									</view>			
								</view>
													
								
								
													
							</view>
						  </view>
						</block>                
					  <view style="display: flex;width: 100%;height: 600rpx" class="flex justify-center align-center text-center" v-if="allTabList.length <= 0">
					  		<u-empty  mode="order"> </u-empty>
					  	</view>
					</scroll-view>
				</swiper-item>       
			</swiper>
		</view>
			
	</view>		
	
</template>

<script>
	import pop from "@/components/pop/pop";
	export default {
		 components: {
		    "regional-selection": pop,
		  },		  
		data() {
			return {	
				...getApp().globalData.config,
				current:0,
				current_set:0,
				currentTab:4,
				allTabList:[], // 全部 
				pageNo:1,
				triggered: false, 
				winHeight:'',
				isLoadMore:false,
				orderId:'',
				tabbar:1,
				list2: [
					{
						name: '全部',
						value:11,
					}, {
						name: '待发货',
						value:2,
					}, {
						name: '待收货',
						value:3,
					},
					{
						name: '已收货',
						value:4,
					},{
						name: '待评价',
						value:5,
					}],
				list1: [
					{
						name: '全部',
						value:11,
					}, {
						name: '待发货',
						value:2,
					}, {
						name: '待收货',
						value:3,
					},
					{
						name: '已收货',
						value:4,
					},{
						name: '待评价',
						value:5,
					}]
}
		},
		onShow() {
			this.allTabList=[]
			this.pageNo=1
			this._requestPageList(true)  
		},	
		
		 onUnload: function () {
		   console.log('ok');
		   this.$routers.router('/pages/mine/index','switchTab') 
		  },
		  
		onBackPress(e) {
			console.log('ok');
			this.$routers.router('/pages/mine/index','switchTab') 
		},
		onLoad(events) {
			console.log('events--------',events)
			if(events.currentTab){
				if(events.currentTab==2) this.current=1;//待发货
				if(events.currentTab==3) this.current=2;//待发货
				if(events.currentTab==4) this.current=3;//待发货
				if(events.currentTab==5) this.current=4;//待发货
				this.currentTab=events.currentTab
			}else{
				this.currentTab=11	
			}
			if(events.orderId) this.orderId=events.orderId
			
			let height = uni.getSystemInfoSync().windowHeight;
			this.winHeight = (height - 164);
			
		},
		methods: {
			...getApp().globalData.function,
			
			return_step(){
				this.$routers.router('/pages/mine/index','switchTab') 
			},
			
			//切换
			onchange(e){
				this.current=0
				this.tabbar=e
				this.allTabList=[]
				this.pageNo=1
				this.currentTab=11
				this._requestPageList(true)  
			},
			
			changeNav(ordertype){
				this.current=ordertype.index
				console.log('ordertype',ordertype);
				this.currentTab=ordertype.value
				this.allTabList=[]
				this.pageNo=1
				this._requestPageList(true)    
			},
			
			// 参数
			_requestPageList(isLoadMore = false, callback) {
			    if(!isLoadMore){
					this.allTabList=[]; // 全部       
					this.pageNo= 1;				
			    }
			    let opt = {
			      pageNo :this.pageNo,
			      token:uni.getStorageSync('userInfo').token,
			      orderType:this.tabbar,
			      status:this.currentTab,	
				  orderId:this.orderId				  
			    }			    
			    this._requestPageListCom("virtualTaskList", opt, callback);
			  },
			_requestPageListCom(url, params, callback) {
			  let that=this
			  this.$http.listIngNew(params).then(res => {
				if (res.resCode == "9999") {
					let oldList = [];     
					oldList = this.allTabList;
					const newGoodsList = res.data.list
					const allData = [...oldList, ...newGoodsList]   
					this.allTabList=allData
					if(callback && typeof callback === 'function'){
						callback()
					}				 
				}
			  })
			
			},
			
			// 上拉加载  滚动到底部/右边时触发
			allLoadMore(e){
				let _self = this;
				if (this._allLoadMore) return
				this._allLoadMore = true;
				this.pageNo= ++_self.pageNo				
				this._requestPageList(true, () => {
				  _self._allLoadMore = false;
				});
			},
			
			// 下拉刷新接口
			onAllRefresh() {
			 // 开启下拉自定义样式
				this.triggered = true;
				this.pageNo=1;
				this.allTabList=[];
				this._requestPageList(false, () => {
					// 当处理完数据刷新后，wx.stopPullDownRefresh可以停止当前页面的下拉刷新
					this.triggered = false;
				});    
			},
			//搜索
			search_sub(){
				uni.navigateTo({
					url:"/pages/order/search"
				})
			},
			
			//详情
			goDetails(orderId){
				uni.navigateTo({
					url:"/pages/pagesuser/shopOrder/goDetails/index?orderId="+orderId
				})
			},
			//归还设备
			backGoods(orderId){
				uni.navigateTo({
					url:"/pages/pagesuser/shopOrder/backGoods/index?orderId="+orderId
				})
			},
			//续租设备
			leaseGoods(orderId){
				uni.navigateTo({
					url:"/pages/pagesuser/shopOrder/leaseGoods/index?orderId="+orderId
				})
			},
			//查看快递
			paykd(orderId){
				uni.navigateTo({
					url:"/pages/pagesuser/shopOrder/orderKd/index?orderId="+orderId
				})
			},
			//再来一单
			payStorage(storageId){				
			   getApp().storageId=storageId
			   uni.switchTab({
				 url: '/pages/class/index',        
			   })  
			},
			
			//申请退款
			apply(orderId){
				uni.navigateTo({
					url:"/pages/pagesuser/shopOrder/apply/index?orderId="+orderId
				})
			},
			//立即评价
		    doevaluate(order_id){
				uni.navigateTo({
					url: '/pages/pagesuser/evaluate/index?orderId='+order_id,        
				}) 
		    },
			
			//加入购物车
			addcar(goods){
				uni.switchTab({
					url: '/pages/class/index',        
				}) 
				// let car={
				// 	goodsId:goods.goods_id,
				// 	specId:goods.goods_spec_id,
				// 	number:1,
				// 	token:uni.getStorageSync('userInfo').token,
				// }
				// this.$http.goodsCar(car).then(res => {
				// 	if(res.resCode=="9999"){
				// 	 uni.showToast({title:res.resMessage,icon: 'none'})      
				// 	}else{					
				// 	  uni.showToast({title:res.resMessage,icon: 'none'})        
				// 	}
				// })
				
			},			
			//取消订单
			cancel(orderId){    
			    this.$showModal({content: '确定要取消订单吗？',confirmColor:'#518FA0'}).then(res=>{
			      if(res.confirm){
			        this.$http.orderCancel({orderId}).then(res=>{
			          this.$showToast(res.resMessage)
			          setTimeout(()=>{
			            this.allTabList=[]
			            this._requestPageList(true);
			          },2000)		          
			          return
			        })       
			      }else{
			      }
			    })
			},
			  
			  
			//确认收货
			payok(order_id,type){
			  let that = this;			   
			  this.$showModal({content: '您确定已经收到货物了吗？',title:'提示',  confirmColor:'#518FA0'}).then(res=>{
				if(res.confirm){
				  let obj_data={
					token:uni.getStorageSync('userInfo').token,
					login_type:'app',
					order_id
				  }
				  this.$http.confirm_receipt(obj_data).then(res=>{
					that.$showToast(res.resMessage) 
					setTimeout(()=>{
						if(type==1){
							that.currentTab=6
							that.current=4	
						}else{
							that.currentTab=6
							that.current=4	
						}						
												
						that.allTabList=[]		
						that.pageNo=1
						that._requestPageList(true);
						
					},3000)          
				  })
				}
			  })
			},
			
			//确认支付
			dopay(goods_total,order_no,order_type,orderId){
				console.log('ok',goods_total)
				
					let that=this
					let obj_data={
					  token:uni.getStorageSync('userInfo').token,
					  pay_no:order_no,
					  pay_type:'wechat',
					  order_type,
					  orderId,
					}	
				    console.log('res',obj_data)
					this.$http.orderpay(obj_data).then(rest=>{
						if(rest.resCode=='9999'){
						    if(rest.resCode=='9999'){
								uni.requestPayment({
									provider: 'wxpay',
									timeStamp: rest.data.timeStamp,
									nonceStr: rest.data.nonceStr,
									package: rest.data.package,
									signType:rest.data.signType,
									paySign: rest.data.paySign,
									success: function (res) {										
										uni.switchTab({
											url:"/pages/order/index"
										})
									},
									fail: function (err) {
										uni.switchTab({
											url:"/pages/order/index"
										})
									},									
								});								   								   
						    }else{
						   	   uni.showToast({title:res.resMessage,icon:"none"}) 
						    }								   
						}else{
						   uni.showToast({title:res.resMessage,icon:"none"}) 
						}	
				   })				   
			}
			
		}
	}
</script>

 
<style>

 @import "./index.css";




</style>
